package volatile关键字;

import java.util.concurrent.CountDownLatch;

/**
 * @program: algorithm
 * @description: 原子性
 * @author: 陈恩涛
 * @create: 2022-08-18 07:59
 **/
public class VolatileAtomicDemo {
    public static void main(String[] args) throws InterruptedException {
        final MyNumber myNumber = new MyNumber();
        final CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    myNumber.addPlusPlus();
                }
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println(myNumber.number);
    }
}

class MyNumber {

    volatile int number;

    public void addPlusPlus() {
        number++;
    }
}
